//
//  MyNumberViewController.m
//  wifi4.2
//
//  Created by 郭四虎 on 15/11/12.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "MyNumberViewController.h"
#import "NumberViewCell.h"
#import "NumberModel.h"
#import "ProductShowController.h"

@interface MyNumberViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dateArr;
    NSInteger nextkey;
}
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UITableView *numTableview;
@property (weak, nonatomic) IBOutlet UILabel *totalNum;

@end

@implementation MyNumberViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [_numTableview registerNib:[UINib nibWithNibName:@"NumberViewCell" bundle:nil] forCellReuseIdentifier:@"NumberViewCell"];
    _dateArr = [NSMutableArray array];
    nextkey = 0;
    [self initUI];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNum) name:@"leftNum" object:nil];
}

- (void)changeNum
{
    if(![Myuser sharedUser].allTotalIntegral.integerValue){
        self.totalNum.text = @"0";
    }else{
        self.totalNum.text = [Myuser sharedUser].allTotalIntegral;
    }
    nextkey = 0;
    [_dateArr removeAllObjects];
    [_numTableview reloadData];
    [self createData];
}

- (void)initUI
{
    if(![Myuser sharedUser].allTotalIntegral.integerValue){
        self.totalNum.text = @"0";
    }else{
        self.totalNum.text = [Myuser sharedUser].allTotalIntegral;
    }
    
    [self.numTableview delsetExtraline];
    [self createData];
    haveMessage(self.numTableview);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"兑换" style:0 target:self action:@selector(myExchange)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)createData
{
    NSDictionary *parameter = @{@"login_id":[Myuser sharedUser].login_id,@"next_key":@(nextkey),@"page_size":@"10"};
    [MyAFN POST:SIGNLIST parameters:parameter block:^(id responseObject) {
        
        if ([[responseObject objectForKey:@"status"]integerValue]) {
            for (NSDictionary *dic in [responseObject objectForKey:@"signList"]) {
                NumberModel *model = [[NumberModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [_dateArr addObject:model];
            }
            if (_dateArr.count) {
                [self.numTableview reloadData];
            }else{
                noMessage(self.numTableview, @"暂时没有积分信息");
            }
        }
        if ([responseObject objectForKey:@"next_key"]) {
            nextkey = [[responseObject objectForKey:@"next_key"]integerValue];
        }
    } error:^(NSError *error) {
        
    } fromController:self];
}

- (void)noMessage
{
    UILabel* _msgLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, WIDTH - 40, 50)];
    _msgLb.text = @"暂时没有积分信息";
    _msgLb.textAlignment = NSTextAlignmentCenter;
    _msgLb.textColor = [UIColor lightGrayColor];
    _msgLb.font = [UIFont systemFontOfSize:18];
    [self.numTableview addSubview:_msgLb];
}

- (void)myBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)myExchange
{
    ProductShowController *controller = [[ProductShowController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dateArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NumberViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NumberViewCell"];
    
    NumberModel *model = _dateArr[indexPath.row];
    [cell showModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
